/**
 * Created by zhangyilin on 2018/12/7.
 */

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.sql.Statement;
import java.text.DecimalFormat;

import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sun.management.snmp.util.SnmpNamedListTableCache;


/**
 * Servlet implementation class showStuTaskContent
 */
@WebServlet("/showStuTaskContent")
public class showStuTaskContent extends HttpServlet{//教师查看显示学生的某一具体任务完成情况
    public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setContentType("text/json; charset=utf-8");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();


        SimpleDateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        Date dateNow = new Date();//获取当前时间
        Date deadline = null;//记录数据库中任务截止日期
        String UserID = request.getParameter("UserID");
        String StudentID = request.getParameter("StudentID");
        String InviteID = request.getParameter("InviteID");
        String taskID = request.getParameter("taskID");
        String taskNum = request.getParameter("num");//仅供显示的任务前的序号，非任务ID
        String StudentName = null;
        String taskTheme = null;
        String taskIssuTime = null;
        String taskDeadline = null;
        String taskContent = null;
        float completeRate = 0;
        DecimalFormat df = new DecimalFormat("#0.0%");//任务进度输出格式调整
        List<String> tasks = new ArrayList<>();//储存逐条查分出来的任务内容
        List<String> completeStatus = new ArrayList<>();//存储每条任务的完成情况
        String url = "jdbc:mysql://localhost:3306/listsystem";//与本机的3306端口建立连接，进入数据库listsystem
        String driverName = "com.mysql.jdbc.Driver";//驱动注册
        String userName = "root";
        String password = "";
        Connection conn = null;//用于建立与数据库的连接
        Statement sta = null;
        ResultSet rs = null;
        try{
            Class.forName(driverName);// jdbc4.0 后无需使用这句进行驱动注册操作
            conn = DriverManager.getConnection(url, userName, password);
        }catch(Exception e){
            System.out.println(e);
        }
        String sql = "select StudentName,TaskTheme,TaskIssuTime,TaskDeadline,TaskContent,CompleteRate from v_task_stu where StudentID='"+StudentID+"' and taskID='"+taskID+"'";
        try {
            if(conn != null) {
                sta = conn.createStatement();
            }
            if(sta != null) {
                rs = sta.executeQuery(sql);
            }
            out.print("<html>");
            out.print("<head>");
            out.print("<title>任务内容</title>");
            out.print("</head>");



            out.print("<meta charset=\"utf-8\" />\n" +
                    "\t\t<title>雨滴特效</title>\n" +
                    "\t\t<style>\n" +
                    "\t\t\tbody {\n" +
                    "\t\t\t\tmargin: 0;\n" +
                    "\t\t\t}\n" +
                    "\t\t\t\n" +
                    "\t\t\tcanvas {\n" +
                    "\t\t\t\tbackground-color: black;\n" +
                    "\t\t\t\tdisplay: block;\n" +
                    "\t\t\t}\n" +
                    "\t\t\t\n" +
                    "\t\t\t#ff {\n" +
                    "\t\t\t\tposition: absolute;\n" +
                    "\t\t\t\ttop: 30%;\n" +
                    "\t\t\t\tleft: 30%;\n" +
                    "\t\t\t\tz-index: 1;\n" +
                    "\t\t\t\ttext-align: center;\n" +
                    "\t\t\t\tcolor: white;\n" +
                    "\t\t\t\tfont: \"楷体\";\n" +
                    "\t\t\t}\n" +
                    "\t\t\t\n" +
                    "\t\t\timg {\n" +
                    "\t\t\t\tposition: absolute;\n" +
                    "\t\t\t\ttop: 10%;\n" +
                    "\t\t\t\tleft: 20%;\n" +
                    "\t\t\t\ttransform: scale(0.6);\n" +
                    "\t\t\t}\n" +
                    "\t\t</style><body>" +
                    "<img src=\"image/welcome.png\" />\n" +
                    "\n" +
                    "\t\t<canvas class='rain'>\n" +
                    "\t\t\t\n" +
                    "\t\t</canvas>");


//            out.print("<body>");

            out.print("<div id='ff'><form action=\"changeCompleteStatus\" method=\"get\">");

            out.print("<center>");
            if(rs != null && rs.next()) {
                StudentName = rs.getString("StudentName");
                taskTheme = rs.getString("TaskTheme");
                taskIssuTime = rs.getString("TaskIssuTime");
                taskDeadline = rs.getString("TaskDeadline");

                try {
                    deadline = dfDate.parse(taskDeadline);
                } catch (ParseException e) {
                    e.printStackTrace();
                }


                taskContent = rs.getString("TaskContent");
                completeRate = Float.parseFloat(rs.getString("CompleteRate"));
                out.print("学号:"+StudentID+"姓名:"+StudentName+"<br>");
//                out.print(taskNum+"."+taskTheme+"进度:"+df.format(completeRate)+"<br>开始时间:"+taskIssuTime+"截止时间:"+taskDeadline+"<br>");


                if(deadline.getTime()>dateNow.getTime()) {//截止日期大于当前日期，任务未截止
                    out.print(taskNum+"."+taskTheme+"进度:"+df.format(completeRate)+"<br>开始时间:"+taskIssuTime+"截止时间:"+taskDeadline+"状态:进行中"+"<br>");
                }
                else if(deadline.getTime()<dateNow.getTime()) {//截止日期小于当前日期，任务截止
                    out.print(taskNum+"."+taskTheme+"进度:"+df.format(completeRate)+"<br>开始时间:"+taskIssuTime+"截止时间:"+taskDeadline+"状态:已截止"+"<br>");
                }

                out.print("任务内容:<br>");
                String[] taskContents = taskContent.split(";");
                for (int i = 0; i < taskContents.length; i++) {
                    String[] temps = taskContents[i].split("@");
                    if(temps.length == 2) {
                        tasks.add(temps[0]);
                        completeStatus.add(temps[1]);
                    }
                }
                for (int i = 0; i < taskContents.length; i++) {
                    if(completeStatus.get(i).equals("N")) {/*输出时设置为disable状态,灰色不可修改,教师不可修改学生任务完成进度*/
                        out.print(tasks.get(i)+"<input type=\"checkbox\" name=\"\" value=\"\" disabled=\"disabled\"><br>");
                    }
                    if(completeStatus.get(i).equals("Y")) {
                        out.print(tasks.get(i)+"<input type=\"checkbox\" name=\"\" value=\"\" checked=\"checked\" disabled=\"disabled\"><br>");
                    }
                }
            }
            out.print("<input type=\"button\" value=\"返回\" onclick=\"window.location.href='showStuTask?UserID="+URLEncoder.encode(UserID)+"&StudentID="+URLEncoder.encode(StudentID)+"&StudentName="+URLEncoder.encode(StudentName)+"&InviteID="+URLEncoder.encode(InviteID)+"&taskID="+URLEncoder.encode(taskID)+"&num="+URLEncoder.encode(taskNum)+"'\">");
            //out.print("<input type=\"button\" value=\"返回\" onclick=\"window.location.href='showStuTask?UserID="+UserID+"&StudentID="+StudentID+"&StudentName="+StudentName+"&InviteID="+InviteID+"&taskID="+taskID+"&num="+taskNum+"'\">");
            out.print("</center>");
            out.print("</form></div>");



            out.print("</body>\t<script>\n" +
                    "\t\tvar canvas = document.querySelector('.rain');\n" +
                    "\t\tvar ctx = canvas.getContext('2d')\n" +
                    "\t\tvar aRain = [];\n" +
                    "\n" +
                    "\t\tvar w = window.innerWidth;\n" +
                    "\t\tvar h = window.innerHeight;\n" +
                    "\t\tcanvas.width = w;\n" +
                    "\t\tcanvas.height = h;\n" +
                    "\t\twindow.onresize = function() {\n" +
                    "\t\t\tcanvas.width = window.innerWidth;\n" +
                    "\t\t\tcanvas.height = window.innerHeight;\n" +
                    "\t\t}\n" +
                    "\n" +
                    "\t\tfunction random(min, max) {\n" +
                    "\t\t\treturn Math.random() * (max - min) + min;\n" +
                    "\t\t}\n" +
                    "\n" +
                    "\t\tfunction Rain() {};\n" +
                    "\t\tRain.prototype = {\n" +
                    "\t\t\tinit: function() {\n" +
                    "\t\t\t\tthis.x = random(0, w);\n" +
                    "\t\t\t\tthis.y = 0;\n" +
                    "\t\t\t\tthis.vy = random(4, 6);\n" +
                    "\t\t\t\tthis.h = random(0.8 * h, 0.9 * h)\n" +
                    "\t\t\t\tthis.r = 1;\n" +
                    "\t\t\t\tthis.vr = 1;\n" +
                    "\t\t\t\tthis.rr = random(60, 130);\n" +
                    "\t\t\t\tthis.ready = true;\n" +
                    "\t\t\t},\n" +
                    "\t\t\tdraw: function() {\n" +
                    "\t\t\t\tif(this.y < this.h && this.ready) {\n" +
                    "\t\t\t\t\tctx.beginPath();\n" +
                    "\t\t\t\t\tctx.fillStyle = '#31f7f7';\n" +
                    "\t\t\t\t\tctx.fillRect(this.x, this.y, 4, 10);\n" +
                    "\t\t\t\t} else {\n" +
                    "\t\t\t\t\tctx.beginPath();\n" +
                    "\t\t\t\t\tctx.strokeStyle = '#31f7f7';\n" +
                    "\t\t\t\t\tctx.arc(this.x, this.y, this.r, 0, Math.PI * 2);\n" +
                    "\t\t\t\t\tctx.stroke();\n" +
                    "\t\t\t\t}\n" +
                    "\t\t\t},\n" +
                    "\t\t\tmove: function() {\n" +
                    "\t\t\t\tif(!this.ready) {\n" +
                    "\t\t\t\t\tif(this.r < 1) {\n" +
                    "\t\t\t\t\t\tthis.ready = true;\n" +
                    "\t\t\t\t\t} else {\n" +
                    "\t\t\t\t\t\tthis.r--;\n" +
                    "\t\t\t\t\t\tthis.draw();\n" +
                    "\t\t\t\t\t\treturn;\n" +
                    "\t\t\t\t\t}\n" +
                    "\t\t\t\t}\n" +
                    "\n" +
                    "\t\t\t\tif(this.y < this.h) {\n" +
                    "\t\t\t\t\tthis.y += this.vy;\n" +
                    "\t\t\t\t} else {\n" +
                    "\t\t\t\t\tthis.r += this.vr;\n" +
                    "\t\t\t\t};\n" +
                    "\t\t\t\tif(this.r < this.rr) {\n" +
                    "\t\t\t\t\tthis.draw();\n" +
                    "\t\t\t\t} else {\n" +
                    "\t\t\t\t\tthis.init();\n" +
                    "\t\t\t\t};\n" +
                    "\t\t\t}\n" +
                    "\t\t}\n" +
                    "\n" +
                    "\t\tsetInterval(function() {\n" +
                    "\t\t\tctx.fillStyle = 'rgba(0,0,0,0.05)';\n" +
                    "\t\t\tctx.fillRect(0, 0, w, h);\n" +
                    "\t\t\tfor(var item of aRain) {\n" +
                    "\t\t\t\titem.move();\n" +
                    "\t\t\t}\n" +
                    "\t\t}, 1000 / 60)\n" +
                    "\n" +
                    "\t\tfunction creatRain(num) {\n" +
                    "\t\t\tfor(var i = 0; i < num; i++) {\n" +
                    "\t\t\t\tsetTimeout(function() {\n" +
                    "\t\t\t\t\tvar rain = new Rain();\n" +
                    "\t\t\t\t\taRain.push(rain);\n" +
                    "\t\t\t\t\train.init();\n" +
                    "\t\t\t\t\train.draw();\n" +
                    "\t\t\t\t}, 200 * i)\n" +
                    "\t\t\t}\n" +
                    "\t\t}\n" +
                    "\n" +
                    "\t\t//\t\tcreatRain(60);\n" +
                    "\t\tcanvas.onmousedown = function(e) {\n" +
                    "\t\t\tvar rain = new Rain();\n" +
                    "\t\t\train.init();\n" +
                    "\t\t\train.x = e.offsetX;\n" +
                    "\t\t\train.y = e.offsetY;\n" +
                    "\t\t\train.r = random(5, 30);\n" +
                    "\t\t\train.ready = false;\n" +
                    "\t\t\taRain.push(rain);\n" +
                    "\t\t}</script></html>");

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if(rs != null)
                    rs.close();
                if(sta != null)
                    sta.close();
                if(conn != null)
                    conn.close();
                rs = null;
                sta = null;
                conn = null;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
